﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using QXGameEngine.Logic;

namespace QXGameEngine.Control {

    public partial class QXMap : UserControl {

        /// <summary>
        /// 地图遮罩控件
        /// </summary>
        public QXMap() {
            InitializeComponent();
        }

        /// <summary>
        /// 设置图片源
        /// </summary>
        public Brush BodySource {
            set { Container.Background = value; }
        }

        /// <summary>
        /// 获取或设置控件主体宽
        /// </summary>
        public double BodyWidth {
            get { return Container.Width; }
            set { Container.Width = value; }
        }

        /// <summary>
        /// 获取或设置控件主体高
        /// </summary>
        public double BodyHeight {
            get { return Container.Height; }
            set { Container.Height = value; }
        }

        /// <summary>
        /// 获取或设置关键中心点与它左上角X距离值
        /// </summary>
        public double CenterX { get; set; }

        /// <summary>
        /// 获取或设置关键中心点与它左上角Y距离值
        /// </summary>
        public double CenterY { get; set; }

        /// <summary>
        /// 获取或设置地图坐标(关联属性)
        /// </summary>
        public Point Coordinate {
            get { return (Point)GetValue(CoordinateProperty); }
            set { SetValue(CoordinateProperty, value); }
        }
        public static readonly DependencyProperty CoordinateProperty = DependencyProperty.Register(
            "Coordinate",
            typeof(Point),
            typeof(QXMap),
            new PropertyMetadata(ChangeCoordinateProperty)
        );
        private static void ChangeCoordinateProperty(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            QXMap obj = (QXMap)d;
            if (obj.Visibility == Visibility.Visible) {
                Point p = (Point)e.NewValue;
                obj.SetValue(Canvas.LeftProperty, p.X - obj.CenterX);
                obj.SetValue(Canvas.TopProperty, p.Y - obj.CenterY);
                obj.SetValue(Canvas.ZIndexProperty, (int)p.Y);
            }
        }
    }
}
